#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using Library.Entity;
using Library.Services;
using System.Collections;
using System.Reflection;

#endregion

namespace Library
{
    public partial class UserControlBook : UserControl
    {
        BookService m_BookService = new BookService();
        User m_currentUser;
        public UserControlBook(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            this.Name = name;
            this.Text = name;
            labelTitle.Text = name;
            m_currentUser = currentUser; 
            initColumn();
            initData();
            initButtonClick();
            toolBarButtonDelete.Enabled = listView.SelectedItem != null;
        }

        private void initButtonClick()
        {
            toolBarButtonAddNew.Click += new EventHandler(toolBarButtonAddNew_Click);
            toolBarButtonDelete.Click += new EventHandler(toolBarButtonDelete_Click);
            listView.ColumnWidthChanged += new ColumnWidthChangedEventHandler(listView_ColumnWidthChanged);
            SettingService.ListViewSettingColumnWidthLoad(listView, this.Name);
        }

        void listView_ColumnWidthChanged(object sender, ColumnWidthChangedEventArgs e)
        {
            SettingService.ListViewSettingColumnWidthSave(listView, this.Name);
        }

        void toolBarButtonDelete_Click(object sender, EventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                MessageBox.Show("Are you sure to delete?", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, new EventHandler(DeleteData));
            }
        }

        void DeleteData(object sender, EventArgs e) 
        {
            try
            {
                MessageBoxWindow msg = (MessageBoxWindow)sender;
                if (msg.DialogResult == DialogResult.Yes)
                {
                    Book Book = (Book)listView.SelectedItem.Tag;
                    if (Book.Id > 0)
                    {
                        m_BookService.DeleteBook(Book);
                        MessageBox.Show("Record has been deleted.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                        listView.Items.Remove(listView.SelectedItem);
                    }
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }

        void toolBarButtonAddNew_Click(object sender, EventArgs e)
        {
            FormBook frm = new FormBook(new Book(), m_currentUser, this);
            frm.ShowDialog();
        }
       
        private void initColumn()
        {
            foreach (FieldInfo pi in (typeof(Book)).GetFields())
            {
                if(!pi.Name.Contains("Id"))
                    listView.Columns.Add(pi.Name, 75, HorizontalAlignment.Left);
            }
           //  listView.Columns.Add("Code", 75, HorizontalAlignment.Left);
           // listView.Columns.Add("Title", 100, HorizontalAlignment.Left);
           // listView.Columns.Add("Remarks", 200, HorizontalAlignment.Left);
        }

        public void initData()
        {
            //IList data = m_BookService.GetAllBook();
            listView.Items.Clear();
            IList data = m_BookService.GetBookByLike(textBoxBookCode.Text);
            foreach (Book book in data)
            {
                //ListViewItem item = listView.Items.Add(Book.Code);
                //item.SubItems.Add(Book.Name);
                //item.SubItems.Add(Book.Remark);
                //item.Tag = Book;
                ArrayList values = new ArrayList();
                foreach (FieldInfo pi in (typeof(Book)).GetFields())
                {
                    if (!pi.Name.Contains("Id"))
                    {
                        object v = book.GetType().GetField(pi.Name).GetValue(book);
                        values.Add(v == null ? "" : v.ToString());
                    }
                }
                string[] vals = (string[])values.ToArray(typeof(string));
                ListViewItem item = listView.Items.Add(book.Code);
                item.SubItems.Clear();
                item.SubItems.AddRange(vals);
                item.Tag = book;
            }
        }

        //internal void AddNewData(Book Book)
        //{
        //    ListViewItem item = listView.Items.Add(Book.Code);
        //    item.SubItems.Add(Book.Name);
        //    item.SubItems.Add(Book.Remark);
        //    item.Tag = Book;
        //}

        private void listView_DoubleClick(object sender, EventArgs e)
        {
            if (listView.SelectedItem != null)
            {
                Book Book = (Book)listView.SelectedItem.Tag;
                FormBook frm = new FormBook(Book, m_currentUser, this);
                frm.ShowDialog();
            }
        }

        //internal void UpdateData(Book Book)
        //{
        //    listView.SelectedItem.SubItems[0].Text = Book.Code;
        //    listView.SelectedItem.SubItems[1].Text = Book.Name;
        //    listView.SelectedItem.SubItems[2].Text = Book.Remark;
        //    listView.SelectedItem.Tag = Book;
        //}

        private void listView_SelectedIndexChanged(object sender, EventArgs e)
        {
            toolBarButtonDelete.Enabled = listView.SelectedItem != null;
        }

        private void textBoxBookCode_EnterKeyDown(object objSender, KeyEventArgs objArgs)
        {
            initData();
        }

        private void buttonFInd_Click(object sender, EventArgs e)
        {
            initData();
        }
    }
}